(function () {

    'use strict';

    angular
        .module('app')
        .controller('ConfirmCardController', ConfirmCardController);

    ConfirmCardController.$inject = ['$scope', '$state', '$rootScope', '$stateParams', 'API', 'ConfirmCartService', 'ExpressSelectService', 'InvoiceSelectService', 'PaymentModel'];


    function ConfirmCardController($scope, $state, $rootScope, $stateParams, API, ConfirmCartService, ExpressSelectService, InvoiceSelectService, PaymentModel) {

        $scope.touchAddress = function() {
            $state.go('address-select', {
                address_id: $scope.address ? $scope.address.address_id : null
            });
        };

        $scope.refreshComment = function (){
            ConfirmCartService.input.comment = $scope.input.comment;
        };

        $scope.card_id = $stateParams.card_id;
        $scope.address = ConfirmCartService.address;
        $scope.input = {
            comment: ""
        };
        $scope.input.comment = ConfirmCartService.input.comment;

        $scope.canPurchase = function() {
            return true;
        };

        $scope.touchSubmit = function() {

            var address = $scope.address;
            if (!address) {
                $scope.toast('请填写地址')
                return;
            }

            var params = {};
            params.card_id=$scope.card_id;
            if ($scope.address) {
                params.address_id = $scope.address.address_id;
            }

            if ($scope.input.comment) {
                params.leave_words = $scope.input.comment;
            }

            API.card.cardExpress(params).then(function (res) {
                if (res) {
                    ConfirmCartService.clear();
                    $state.go('my-card', {});
                }
            });
        }

    }

})();